package com.retoc.oawx.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retoc.oawx.domain.Evection;
import com.retoc.oawx.exception.OAException;
import com.retoc.oawx.mapper.UserMapper;
import com.retoc.oawx.service.ApproveService;
import com.retoc.oawx.service.EvectionService;
import com.retoc.oawx.mapper.EvectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
* @author retoc
* @description 针对表【tb_evection】的数据库操作Service实现
* @createDate 2022-06-23 18:10:02
*/
@Service
public class EvectionServiceImpl extends ServiceImpl<EvectionMapper, Evection>
    implements EvectionService{

    @Autowired
    UserMapper userMapper;

    @Autowired
    EvectionMapper evectionMapper;

    @Autowired
    ApproveService approveService;

    @Value("${oawx.receiveNotify}")
    private String receiveNotify;

    @Value("${workflow.urla}")
    private String workflow;

    @Override
    public int createEvection(Evection evection) {
        int gmId = userMapper.searchGmId();
        if (gmId==evection.getCreaterId()){
            evection.setStatus(1);
        }
        int row = evectionMapper.createEvection(evection);
        if (row!=1){
            throw new OAException("创建用车单失败");
        }
        startWorkflow(evection);
        return row;
    }

    @Override
    public Evection selectEvectionByInstanceId(String instanceId) {
        return evectionMapper.selectEvectionByInstanceId(instanceId);
    }

    @Override
    public ArrayList<HashMap> selectEvectionListByCreator(HashMap map) {
        return evectionMapper.selectEvectionListByCreator(map);
    }

    @Override
    public ArrayList<HashMap> selectOldEvectionListByCreator(HashMap map) {
        return evectionMapper.selectOldEvectionListByCreator(map);
    }

    @Override
    public void approveEvectionByInstanceId(String instanceId) {
        evectionMapper.approveEvectionByInstanceId(instanceId);
    }

    @Override
    public HashMap selectEvectionById(Integer id) {
        return evectionMapper.selectEvectionById(id);
    }

    @Override
    public Evection selectEvectionById1(Integer id) {
        return evectionMapper.selectEvectionById1(id);
    }

    @Override
    public int deleteByid(Integer id) {
        HashMap hashMap = evectionMapper.selectEvectionById(id);
        String instanceId =(String) hashMap.get("instanceId");
        approveService.deleteProcess(instanceId);
        int row = evectionMapper.deleteEvectionId(id);
        return row;
    }

    @Override
    public int updateEvection(Evection evection) {
        approveService.deleteProcess(evection.getInstanceId());
        int i=evectionMapper.updateEvection(evection);
        startWorkflow(evection);
        return i;
    }


    public void startWorkflow(Evection evection){
        JSONObject json = new JSONObject();
        json.set("url", receiveNotify);
        json.set("createrId", evection.getCreaterId());
        int deptManagerId = userMapper.searchDeptManagerId(evection.getCreaterId());
        json.set("managerId", deptManagerId);
        json.set("gmId",userMapper.searchGmId());
        json.set("adId",4);
        Date startTime = evection.getStartTime();
        Date endTime = evection.getEndTime();
        json.set("dates", startTime);
        long between = DateUtil.between(startTime, endTime, DateUnit.DAY);
        int needGm=0;
        if (evection.getCreaterId()==deptManagerId){
            needGm=1;
        }
        if (between > 3) {
            needGm=1;
        }
        json.set("needGm", needGm);
        json.set("types", "evection");
        String url = workflow + "/evection/evectionRequest";
        HttpResponse resp = HttpRequest.post(url).header("Content-Type", "application/json").body(json.toString())
                .execute();
        if (resp.getStatus() == 200) {
            JSONObject json1 = JSONUtil.parseObj(resp.body());
            JSONObject result = JSONUtil.parseObj(json1.getStr("result"));
            String instanceId = result.getStr("instanceId");
            HashMap param = new HashMap();
            param.put("uuid", evection.getUuid());
            param.put("instanceId", instanceId);
            int row1 = evectionMapper.setInstanceId(param);
            if (row1!=1){
                throw new OAException("保存会议工作流失败");
            }
        }
    }
}




